package view
{
	import data.Globals;
	import data.MonsterProp;
	import data.WaveProp;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import manager.*;
	
	import utils.GameUtils;
	
	import view.Arm.BaseTank;
	import view.base.ClearView;
	import view.monster.BaseMonster;
	import view.plant.PlantItem;
	
	public class MonsterView extends ClearView
	{
		public var aMonster:Array;
		
		public var selecet:BaseMonster;
		
		private var queue:Vector.<MonsterProp>;
		
		/**
		 *距离上一波怪兽出现时间 
		 */
		private var waveGap:Number = 5;
		/**
		 * 当前一波怪兽 
		 */
		private var curWave:WaveProp;
		
		private var curProp:MonsterProp;
		
		/**
		 *第几波 
		 */
		private var waveNum:int = 1;
		
		private var monster_go:String;
		
		private var wave_gap:String;
		
		/**
		 * 是否满足检测升级条件 
		 */
		private var isCanCheckUpCondition:Boolean;
		
		private var timeObj: Object;
		
		private var timeCall:Function;
		
		public function MonsterView()
		{
			super();
			aMonster = []; 
			timeObj = {};
			this.addEventListener(MouseEvent.CLICK,clickHandle);
			this.addEventListener(MouseEvent.MOUSE_OVER,overHandle);
			this.addEventListener(MouseEvent.MOUSE_OUT,outHandle);
		}
		
		protected function overHandle(event:MouseEvent):void
		{
			var monster:BaseMonster = event.target as BaseMonster;
			if(monster){		
				Globals.gameView.showMonsterTip(true,monster.x+5,monster.y,monster.blood,monster.energy,monster.exp_rad,monster.hurt);
			}
		}
		
		protected function outHandle(event:MouseEvent):void
		{
			Globals.gameView.showMonsterTip(false);
		}
		
		protected function clickHandle(event:MouseEvent):void
		{
			selecet = event.target as BaseMonster;
			Globals.gameView.targetPlant = null;
		}
		
		public function initMonster():void
		{
			if(!queue){
				getQueue();
				waitWaveGap();
				trace("第"+waveNum+"波！");
				Globals.gameView.setWave(waveNum,QueueManage.getInstance().queueNum);
				return;
			}else if(!queue.length){
				getQueue();
				if(curWave){
					waveNum++;
					waitWaveGap();
				}else{
					//当前等级怪物队列结束，检测升级条件
					isCanCheckUpCondition = true;
					return;
				}
				trace("第"+waveNum+"波！");
				Globals.gameView.setWave(waveNum,QueueManage.getInstance().queueNum);
				return;
			}
			curProp = queue.shift();
			monster_go = m_setTimeout(monsterGo,curProp.gapTime);
			waveGap+=curProp.gapTime;
		}
		
		override public function clearView():void{
			for each(var monster:BaseMonster in aMonster){
				monster.die();
			}
			super.clearView();
			aMonster.length = 0;
			queue = null;
			m_clearTimeout(monster_go);
			m_clearTimeout(wave_gap);
			waveNum = 1;
			curWave = null;
			curProp = null;
			isCanCheckUpCondition = false;
		}
		
		private function waitWaveGap():void{
			wave_gap = m_setTimeout(initMonster,waveGap);
		}

		private function getQueue():void{
			curWave = QueueManage.getInstance().getQueue();
			if(curWave){
				queue = curWave.list;
				waveGap = 12;
				if(waveNum%8==0){
					Globals.gameView.initPlant();
				}
			}
		}
		
		private function monsterGo():void
		{
			var a:BaseMonster = ObjectPool.getObj(MonsterName.getMonsterCls(curProp.name)) as BaseMonster;
			this.addChild(a);
			a.onRemove = deleteThis;
			a.name = curProp.name;
			a.blood = curProp.blood;
			a.speed = curProp.speed;
			a.exp_rad = curProp.exp_rad;
			a.hurt = curProp.hurt;
			a.energy = curProp.energy;
			//怪物重置
			a.resetData();
			aMonster.push(a);
			initMonster();
		}
		
		private function deleteThis(item:BaseMonster):void
		{
			aMonster.splice(aMonster.indexOf(item),1);
			var tankAry:Array = Globals.gameView.getTankAry();
			var lens:int;
			var i:int = 0,len:int = tankAry.length;
			for(i;i<len;i++){
				var tank:BaseTank = tankAry[i];
				if(tank){
					lens = GameUtils.getTwoPointDistance(new Point(item.x,item.y),new Point(tank.x,tank.y));
					if(lens<item.exp_rad){
						tank.blood-=item.hurt;
					}
				}
			}
			var plantAry:Array = Globals.gameView.getPlantAry();
			lens = 0;
			i = 0;
			len = plantAry.length;
			for(i;i<len;i++){
				var plant:PlantItem = plantAry[i];
				if(plant){
					lens = GameUtils.getTwoPointDistance(new Point(item.x,item.y),new Point(plant.x,plant.y));
					if(lens<item.exp_rad){
						plant.energy-=item.hurt/3;
					}
				}
			}
		}
		
		override protected function loop(event:Event):void{
			for each(var monster:BaseMonster in aMonster){
				monster.move();
			}
			if(isCanCheckUpCondition){
				if(aMonster.length == 0){
					isCanCheckUpCondition = false;
					if(Globals.totalLevel&&Globals.totalLevel == LevelManange.getInstance().curLevel){
						Globals.towerWar.gameWin();
					}else{	
						LevelManange.getInstance().curLevel++;
						LevelManange.getInstance().userLevel++;
						Globals.towerWar.gameUpLevel(false);
					}
				}
			}
			var o:Object;
			for(var key:String in timeObj){
				o = timeObj[key];
				o.time++;
				if(o.time/30>=o.gap_time){
					o.timeCall();
					m_clearTimeout(key);
				}
			}
			
		}
		
		private function m_setTimeout(callback:Function,delayCount:int):String{
			var obj:Object = {};
			obj.timeCall = callback;
			obj.gap_time = delayCount;
			obj.time = 0;
			var time:String = getTimer()+'';
			while(timeObj[time]){
				time = int(time)+Math.random()*100+'';
			}
		    timeObj[time] = obj;
			return time;
		}
		
		private function m_clearTimeout(value:String):void{
			if(timeObj[value]){
				timeObj[value] = null;
				delete timeObj[value];
			}
		}
	}
}